/***
This do-file plots the evolution of the slope between the change of low-wage
employment and median 2-bedroom rent in the period 2020-2021.
***/

*-------------------------------------------------------------------------------
* Set up
*-------------------------------------------------------------------------------

* Set $root
project figstabs, root
if (r(buildrunning)==0) include "${root}/code/config_interactive.do"

* Set globals
project, uses("${root}/code/set_globals.do")
include "${root}/code/set_globals.do"

* Create required subfolders
cap mkdir "${root}/results/Employment"
cap mkdir "${root}/results/Small Business Revenue"
cap mkdir "${root}/results/Jobs"

*-------------------------------------------------------------------------------
* Q1 Employment slope evolution over time
*-------------------------------------------------------------------------------

* Import Employment data
project, uses("${root}/data/web/data/Employment - County - Weekly.csv")
import delimited "${root}/data/web/data/Employment - County - Weekly.csv", clear

* Drop CA, MA, NY
gen statefips = floor(countyfips / 1000)
drop if inlist(statefips, 6, 25, 36)

gen date = mdy(month, day_endofweek, year)

* Make a percentage change
replace emp_incq1 = emp_incq1 * 100

* Take mean of month
local i = 4

foreach month in april may june july august september october november december {
	gegen temp = mean(emp_incq1) if year == 2020 & month == `i', by(countyfips)
	gegen change_emp_`month'_2020 = mean(temp), by(countyfips)
	drop temp
	local i = `i' + 1
}

local i = 1
foreach month in january february march april may june july august september october november december {
	gegen temp = mean(emp_incq1) if year == 2021 & month == `i', by(countyfips)
	gegen change_emp_`month'_2021 = mean(temp) , by(countyfips)
	drop temp
	local i = `i' + 1
}

* Collapse
gcollapse (mean) change_emp_* , by(countyfips)

* Merge population
rename countyfips county_fips
project, uses("${root}/data/derived/ACS 2014-2018 5-Year County/ACS 2014-2018 County.dta")
merge 1:1 county_fips using "${root}/data/derived/ACS 2014-2018 5-Year County/ACS 2014-2018 County.dta", keepusing(med_2br_2014_2018_est pop_2014_2018_est)  keep(3) nogen
rename county_fips countyfips
rename (med_2br_2014_2018_est pop_2014_2018_est) (median_2br_rent_2018 pop_2018)

* Run regressions

foreach month in april may june july august september october november december {
	reg change_emp_`month'_2020 median_2br_rent_2018 [w = pop_2018], r
		local beta = _b[median_2br_rent_2018] * 1000
		local beta_`month'_2020: display %4.2f `beta'
		local std_err = _se[median_2br_rent_2018] * 1000
		local se_`month'_2020: display %4.2f `std_err'
		local lb_`month'_2020 = (_b[median_2br_rent_2018] - invttail(e(df_r), 0.025) * _se[median_2br_rent_2018]) * 1000
		local ub_`month'_2020 = (_b[median_2br_rent_2018] + invttail(e(df_r), 0.025) * _se[median_2br_rent_2018]) * 1000
}

foreach month in january february march april may june july august september october november december{ // april may june july august september october november {
	reg change_emp_`month'_2021 median_2br_rent_2018 [w = pop_2018], r
		local beta = _b[median_2br_rent_2018] * 1000
		local beta_`month'_2021: display %4.2f `beta'
		local std_err = _se[median_2br_rent_2018] * 1000
		local se_`month'_2021: display %4.2f `std_err'
		local lb_`month'_2021 = (_b[median_2br_rent_2018] - invttail(e(df_r), 0.025) * _se[median_2br_rent_2018]) * 1000
		local ub_`month'_2021 = (_b[median_2br_rent_2018] + invttail(e(df_r), 0.025) * _se[median_2br_rent_2018]) * 1000

}

local unit "/$1000"

* Plot December 2021
binscatter change_emp_december_2021 median_2br_rent_2018 [w = pop_2018] ///
	, ///
	${title_`version'} ///
	color(oi1) lcolor(oi2) ///
	ytitle("Change in Low-Wage Employment (%)" "from January 2020 to December 2021") ///
	ylab(-10 "-10%" -20 "-20%" -30 "-30%", nogrid) ///
	xtitle("Median Two Bedroom Monthly Rent in 2014-2018 ($)") ///
	xsize(10) ///
	xlab(500 "$500" 1000 "$1,000" 1500 "$1,500" 2000 "$2,000") ///
	xsc(ra(500 2050)) ///
	text(-31 500 "Slope = `beta_december_2021'%`unit' (s.e. = `se_december_2021')", place(ne) just(left) color(gs8) size(medlarge)) ///
	legend(off)
	oi_graph_export "${root}/results/Employment/Employment vs Rent - December 2021",  type(${fig_type})

* Black and white version for QJE
binscatter change_emp_december_2021 median_2br_rent_2018 [w = pop_2018] ///
	, ///
	${title_`version'} ///
	color(gs0) lcolor(gs8) ///
	ytitle("Change in Low-Wage Employment (%)" "from January 2020 to December 2021") ///
	ylab(-10 "-10%" -20 "-20%" -30 "-30%", nogrid) ///
	xtitle("Median Two Bedroom Monthly Rent in 2014-2018 ($)") ///
	xsize(10) ///
	xlab(500 "$500" 1000 "$1,000" 1500 "$1,500" 2000 "$2,000") ///
	xsc(ra(500 2050)) ///
	text(-31 500 "Slope = `beta_december_2021'%`unit' (s.e. = `se_december_2021')", place(ne) just(left) color(gs8) size(medlarge)) ///
	legend(off)

graph export "${root}/results/QJE_Figures_BlackAndWhite/Figure_6b.svg", replace
project, creates("${root}/results/QJE_Figures_BlackAndWhite/Figure_6b.svg")

*------------------------------------------------------------------------------*
* Output numbers for paper
*------------------------------------------------------------------------------*
* Rent quantiles
gquantiles rent_ventile = median_2br_rent_2018 [w = pop_2018] if !mi(change_emp_december_2021), xtile nq(20)

* Emp changes in lowest and highest rent ventiles in Dec 2021
sum change_emp_december_2021 [w = pop_2018] if rent_ventile == 1
local emp_dec_rent_p5: di %3.1f abs(r(mean))
sum change_emp_december_2021 [w = pop_2018] if rent_ventile == 20
local emp_dec_rent_p100: di %3.1f abs(r(mean))

di `emp_dec_rent_p5'
di `emp_dec_rent_p100'

* Export numbers
cap erase "${root}/results/paper numbers/Employment/Q1 Employment Change by Rent Ventile in Dec 2021.yaml"

yamlout using "${root}/results/paper numbers/Employment/Q1 Employment Change by Rent Ventile in Dec 2021.yaml", ///
	key("emp_dec_rent_p5") ///
	comment("Emp change in Dec 2021, first rent ventile") ///
	value(`emp_dec_rent_p5') fmt(%2.0f)

yamlout using "${root}/results/paper numbers/Employment/Q1 Employment Change by Rent Ventile in Dec 2021.yaml", ///
	key("emp_dec_rent_p100") ///
	comment("Emp change in Dec 2021, last rent ventile") ///
	value(`emp_dec_rent_p100') fmt(%2.0f)

project, creates("${root}/results/paper numbers/Employment/Q1 Employment Change by Rent Ventile in Dec 2021.yaml")

*-------------------------------------------------------------------------------
* Plot
*-------------------------------------------------------------------------------

* Import Employment data
project, uses("${root}/data/web/data/Employment - County - Weekly.csv")
import delimited "${root}/data/web/data/Employment - County - Weekly.csv", clear
keep year month
gduplicates drop

gen slope = `beta_april_2020' if year == 2020 & month == 4
replace slope = `beta_may_2020' if year == 2020 & month == 5
replace slope = `beta_june_2020' if year == 2020 & month == 6
replace slope = `beta_july_2020' if year == 2020 & month == 7
replace slope = `beta_august_2020' if year == 2020 & month == 8
replace slope = `beta_september_2020' if year == 2020 & month == 9
replace slope = `beta_october_2020' if year == 2020 & month == 10
replace slope = `beta_november_2020' if year == 2020 & month == 11
replace slope = `beta_december_2020' if year == 2020 & month == 12

replace slope = `beta_january_2021' if year == 2021 & month == 1
replace slope = `beta_february_2021' if year == 2021 & month == 2
replace slope = `beta_march_2021' if year == 2021 & month == 3
replace slope = `beta_april_2021' if year == 2021 & month == 4
replace slope = `beta_may_2021' if year == 2021 & month == 5
replace slope = `beta_june_2021' if year == 2021 & month == 6
replace slope = `beta_july_2021' if year == 2021 & month == 7
replace slope = `beta_august_2021' if year == 2021 & month == 8
replace slope = `beta_september_2021' if year == 2021 & month == 9
replace slope = `beta_october_2021' if year == 2021 & month == 10
replace slope = `beta_november_2021' if year == 2021 & month == 11
replace slope = `beta_december_2021' if year == 2021 & month == 12


gen lower_bound = `lb_april_2020' if year == 2020 & month == 4
replace lower_bound = `lb_may_2020' if year == 2020 & month == 5
replace lower_bound = `lb_june_2020' if year == 2020 & month == 6
replace lower_bound = `lb_july_2020' if year == 2020 & month == 7
replace lower_bound = `lb_august_2020' if year == 2020 & month == 8
replace lower_bound = `lb_september_2020' if year == 2020 & month == 9
replace lower_bound = `lb_october_2020' if year == 2020 & month == 10
replace lower_bound = `lb_november_2020' if year == 2020 & month == 11
replace lower_bound = `lb_december_2020' if year == 2020 & month == 12

replace lower_bound = `lb_january_2021' if year == 2021 & month == 1
replace lower_bound = `lb_february_2021' if year == 2021 & month == 2
replace lower_bound = `lb_march_2021' if year == 2021 & month == 3
replace lower_bound = `lb_april_2021' if year == 2021 & month == 4
replace lower_bound = `lb_may_2021' if year == 2021 & month == 5
replace lower_bound = `lb_june_2021' if year == 2021 & month == 6
replace lower_bound = `lb_july_2021' if year == 2021 & month == 7
replace lower_bound = `lb_august_2021' if year == 2021 & month == 8
replace lower_bound = `lb_september_2021' if year == 2021 & month == 9
replace lower_bound = `lb_october_2021' if year == 2021 & month == 10
replace lower_bound = `lb_november_2021' if year == 2021 & month == 11
replace lower_bound = `lb_december_2021' if year == 2021 & month == 12

gen upper_bound = `ub_april_2020' if year == 2020 & month == 4
replace upper_bound = `ub_may_2020' if year == 2020 & month == 5
replace upper_bound = `ub_june_2020' if year == 2020 & month == 6
replace upper_bound = `ub_july_2020' if year == 2020 & month == 7
replace upper_bound = `ub_august_2020' if year == 2020 & month == 8
replace upper_bound = `ub_september_2020' if year == 2020 & month == 9
replace upper_bound = `ub_october_2020' if year == 2020 & month == 10
replace upper_bound = `ub_november_2020' if year == 2020 & month == 11
replace upper_bound = `ub_december_2020' if year == 2020 & month == 12

replace upper_bound = `ub_january_2021' if year == 2021 & month == 1
replace upper_bound = `ub_february_2021' if year == 2021 & month == 2
replace upper_bound = `ub_march_2021' if year == 2021 & month == 3
replace upper_bound = `ub_april_2021' if year == 2021 & month == 4
replace upper_bound = `ub_may_2021' if year == 2021 & month == 5
replace upper_bound = `ub_june_2021' if year == 2021 & month == 6
replace upper_bound = `ub_july_2021' if year == 2021 & month == 7
replace upper_bound = `ub_august_2021' if year == 2021 & month == 8
replace upper_bound = `ub_september_2021' if year == 2021 & month == 9
replace upper_bound = `ub_october_2021' if year == 2021 & month == 10
replace upper_bound = `ub_november_2021' if year == 2021 & month == 11
replace upper_bound = `ub_december_2021' if year == 2021 & month == 12

gen date = ym(year, month)
sort date

tw (line slope date if slope != .) ///
   (line lower_bound date if lower_bound != ., lpattern(dash) lcolor(oi1%50)) ///
   (line upper_bound date if upper_bound != ., lpattern(dash) lcolor(oi1%50)) , ///
	ytitle("Change in Low-Wage Employment" "vs Rent Slope (%/$1000)") ///
	xtitle("") ///
	legend(order(2 "95% Confidence Interval") cols(1) pos(5) ring(0) size(small) symxsize(size)) ///
	ylab( 0 "0%" -5 "-5%" -10 "-10%" -15 "-15%" -20 "-20%" -25 "-25%", nogrid) ///
	yline(0, lpattern(dash) lcolor(black)) ///
	xlabel(`=ym(2020, 5)' `""May" "2020""'  ///
	  `=ym(2020, 7)' "Jul"  ///
	`=ym(2020, 9)' "Sep"  `=ym(2020, 11)' "Nov" ///
	`=ym(2021, 1)' `""Jan" "2021""'  ///
	 `=ym(2021, 3)' "Mar"  `=ym(2021, 5)' "May" ///
	 `=ym(2021, 7)' "Jul"  ///
	`=ym(2021, 9)' "Sep"  `=ym(2021, 11)' "Nov"   , labsize(medsmall))

oi_graph_export "${root}/results/Employment/Employment - Evolution of rent gradient - Q1 Employment", type(${fig_type})

* Black and white version for QJE
tw (line slope date if slope != ., lcolor(gs0)) ///
   (line lower_bound date if lower_bound != ., lpattern(dash) lcolor(gs0%50)) ///
   (line upper_bound date if upper_bound != ., lpattern(dash) lcolor(gs0%50)) , ///
	ytitle("Change in Low-Wage Employment" "vs Rent Slope (%/$1000)") ///
	xtitle("") ///
	legend(order(2 "95% Confidence Interval") cols(1) pos(5) ring(0) size(small) symxsize(size)) ///
	ylab( 0 "0%" -5 "-5%" -10 "-10%" -15 "-15%" -20 "-20%" -25 "-25%", nogrid) ///
	yline(0, lpattern(dash) lcolor(black)) ///
	xlabel(`=ym(2020, 5)' `""May" "2020""'  ///
	  `=ym(2020, 7)' "Jul"  ///
	`=ym(2020, 9)' "Sep"  `=ym(2020, 11)' "Nov" ///
	`=ym(2021, 1)' `""Jan" "2021""'  ///
	 `=ym(2021, 3)' "Mar"  `=ym(2021, 5)' "May" ///
	 `=ym(2021, 7)' "Jul"  ///
	`=ym(2021, 9)' "Sep"  `=ym(2021, 11)' "Nov"   , labsize(medsmall))

graph export "${root}/results/QJE_Figures_BlackAndWhite/Figure_6d.svg", replace
project, creates("${root}/results/QJE_Figures_BlackAndWhite/Figure_6d.svg")
